package cn.com.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import cn.com.domain.Lifebk;

/**
 * 生活百科Service
 * @author ChonZY
 */
public class LifebkService {
	/**
	 * 获得短信黄页 数据
	 * @param strUrl
	 * @return
	 */
	public String connServerForResult(String strUrl) {
		HttpGet httpRequest = new HttpGet(strUrl);
		String strResult = "";
		try {
			HttpClient httpClient = new DefaultHttpClient();
			// 获得 httpResponse 对象
			HttpResponse response = httpClient.execute(httpRequest);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strResult;
	}
	/**
	 * 解析短信黄页json 数据
	 * @param strResult
	 * @return
	 */
    public List<Lifebk> parseJson_DXHY(String strResult) { 
    	List<Lifebk> lifeBks = null ; // 短信黄页集合
        try { 
            JSONArray jsonObjs = new JSONObject(strResult).getJSONArray("data"); 
            Lifebk bk = null;
            lifeBks = new ArrayList<Lifebk>();
            if(jsonObjs !=null){
            	for(int i = 0; i < jsonObjs.length() ; i++){ 
            		int id = jsonObjs.getJSONObject(i).getInt("id");
            		bk =  new Lifebk();
            		bk.setId(id);
            		String name = jsonObjs.getJSONObject(i).getString("name");
            		bk.setName(name);
            		String count = jsonObjs.getJSONObject(i).getString("count");
            		bk.setCount(count);
            		lifeBks.add(bk);
            	} 
            }
        } catch (JSONException e) { 
        	  Log.i("Other", "短信黄页出错");
            e.printStackTrace(); 
        }
		return lifeBks; 
    } 

}
